<?php

defined('IN_XLP') or exit('Access Denied');
/*
 * 小管家抵价券模型
 */

class steCouponModel extends Model {

    function __construct() {
        parent::__construct();
        $this->dbTable = 'ste_coupon';
    }

    /*
     * 获取我的可用抵价券
     */

    public function getMyCoupon($uid, $cityId) {
        $rs = $this->field('stype,smod,coupon_title,coupon_code,start_amount,'
                                . 'coupon_money,end_time')
                        ->where(array('used_uid' => $uid, 'is_used' => 0, 'city_id' => $cityId,
                            'used_time' => 0, 'start_time[<=]' => TIME, 'end_time[>=]' => TIME))->findAll();
        if ($rs) {
            return array('rs' => $rs, 'total' => $this->getTotal());
        } else {
            return array('rs' => array(), 'total' => 0);
        }
    }

    /*
     * 检查抵价券是否可用
     * @param $couponUsed string 抵价券码
     * @param $cityId int 用户城市id
     * @return array array('status'=>0,'info'=>'','offset'=>'');
     */

    public function checkCoupon($couponUsed, $orderAmount, $cityId, $uid, $isNoNew = false) {
        $rs = $this->where(array('coupon_code' => $couponUsed, 'city_id' => $cityId))->find();
        if (!$rs) {
            return array('status' => 0, 'info' => '抵价券不存在');
        }
        if ($rs['is_used']) {
            return array('status' => 0, 'info' => '该券已失效');
        }
        if ($rs['start_time'] && $rs['start_time'] > TIME) {
            return array('status' => 0, 'info' => '该券尚未生效');
        }
        if ($rs['end_time'] && $rs['end_time'] < TIME) {
            return array('status' => 0, 'info' => '该券已过期');
        }
        if ($rs['start_amount'] > $orderAmount) {
            return array('status' => 0, 'info' => '订单满' . $rs['start_amount'] . '元才可使用');
        }
        if ($rs['used_uid'] && $rs['used_uid'] != $uid) {
            return array('status' => 0, 'info' => '该券已绑定到其他帐户');
        }
        //新人券
        if (!$isNoNew && $rs['stype'] == 2 && !D('steorder')->checkIsNewShopper($uid)) {
            return array('status' => 0, 'info' => '您不满足新人券使用条件');
        }
        /*
         * 临时加上，检查他妈的那50张券是否被重复使用
         */
        if ($rs['info_time'] == 1421660957 && $this->checkOneDayCoupon($uid, $rs['info_time'])) {
            return array('status' => 0, 'info' => '抱歉，该券每帐号仅限使用1次');
        }
        /*
         * 临时加上，限制每天只能使用一张
         */
        $stime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $etime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        if ($this->where(array('used_uid' => $uid, 'city_id' => $cityId, 'used_time[>=]' => $stime, 'used_time[<=]' => $etime, 'is_used' => 1))->count()) {
            return array('status' => 0, 'info' => '每天只能使用一张无限制抵价券');
        }
        return array('status' => 1, 'info' => 'ok', 'type' => $rs['stype'], 'offset' => $rs['coupon_money']);
    }

    /*
     * 处理抵价券
     * @param $coupon string 抵价券码
     * @param $cityId int 用户城市id
     * @param $orderSn string 订单号
     * @param $uid int 使用者uid
     * @return array
     */

    public function setUserCoupon($coupon, $orderAmount, $cityId, $orderSn, $uid) {
        $check = $this->checkCoupon($coupon, $orderAmount, $cityId, $uid, true);
        if (!$check['status']) {
            return $check;
        }
        $info = $this->update(array('is_used' => 1, 'used_uid' => $uid, 'used_time' => TIME,
            'order_sn' => $orderSn), array('coupon_code' => $coupon));
        return array('status' => 1, 'info' => $info);
    }

    /*
     * 退还抵价券,仅限用户及管理员取消订单使用
     * @param $orderSn string 订单号
     * @return array 处理结果
     */

    public function cancelCoupon($orderSn, $uid) {
        $rs = $this->field('id')->where(array('used_uid' => $uid, 'order_sn' => $orderSn))->find();
        if (!$rs) {
            return array('status' => 0, 'info' => '抵价券不存在');
        }
        return array('status' => 1, 'info' => $this->update(array('is_used' => 0,
                'used_time' => 0, 'order_sn' => ''), array('id' => $rs['id'])));
    }

    /*
     * 随机发放指定类型的券,只限发放红包等随机抽取操作
     * @param $uid int UID
     * @param $cityId int 城市ID
     * @param $type int 抵价券类型
     * @param $smod int 小类
     * @param $limit int 限领数量
     * return array
     */

    public function addRandCoupon($uid, $cityId, $type = 0, $smod = 0, $couponMoney = 0, $limit = 0) {
        //先判断是否领取了该条件下的券
        if ($limit && $this->where(array('used_uid' => $uid, 'stype' => $type, 'city_id' => $cityId, 'smod' => $smod))->count() >= $limit) {
            return array('status' => 0, 'msg' => '您已经领取过啦');
        }
        //抽取一张符合条件的券
        $rs = $this->where(array('used_uid' => 0, 'stype' => $type, 'city_id' => $cityId, 'smod' => $smod,
                    'is_used' => 0, 'coupon_money' => $couponMoney))->find();
        if (!$rs) {//说明券发放完毕
            return array('status' => 0, 'msg' => '券领完了');
        } else {
            if ($this->update(array('used_uid' => $uid), array('id' => $rs['id']))) {
                return array('status' => 1, 'rs' => $rs, 'msg' => 'ok');
            } else {
                return array('status' => 0, 'msg' => '领取失败，请重试');
            }
        }
    }

    /*
     * 二维码扫描领券
     * @param $objData array 生成的券组数据
     */

    public function addPrizeCoupon($objData) {
        $data = array(
            'stype' => 1,
            'smod' => 0,
            'city_id' => 0,
            'coupon_title' => '',
            'coupon_code' => '',
            'start_amount' => 0,
            'coupon_money' => 0,
            'is_used' => 0,
            'used_uid' => 0,
            'used_time' => 0,
            'order_sn' => '',
            'start_time' => 0,
            'end_time' => 0,
            'info_time' => 0
        );
        return $this->insert(array_merge($data, $objData));
    }

    /*
     * 生成优惠券码，不可与现有券码重合
     */

    public function getCouponCode() {
        $code = getRandInt();
        if ($this->field('id')->where(array('coupon_code' => $code))->find()) {
            $code = $this->getCouponCode();
        }
        return $code;
    }

    /*
     * 检查同日期的抵价券是否有使用过
     */

    public function checkOneDayCoupon($uid, $infotime) {
        return $this->field('id')->where(array('used_uid' => $uid, 'is_used' => 1, 'info_time' => $infotime))->find() ? true : false;
    }

}
